;; Tests of the non-robust computational geometry functions
;; Copyright (C) 2021  Rocks Mazama
;; 
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; 
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;; 
;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(ns mazama.compgeom.convex-hull.jarvis-march-test
  (:require
   [clojure.test :refer [deftest is testing]]
   [mazama.compgeom.convex-hull.jarvis-march :refer [convex-hull]]
   [mazama.compgeom.poly :refer [polygon==]]))

;;;; NOTICE
;;;;
;;;; It is assumed that
;;;; the x-axis increases to the right and
;;;; the y-axis increases DOWNWARD

(deftest small-test
  (testing "zero"
    (let [S []]
      (is (polygon== S (convex-hull S)))))
  (testing "one"
    (let [S [[0 1]]]
      (is (polygon== S (convex-hull S)))))
  (testing "two"
    (let [S [[-2 -3] [0 1]]]
      (is (polygon== S (convex-hull S))))))

(deftest collinear-test
  (testing "simple axis-aligned box with collinear sides"
    (let [nodes [[1 0] [0 0] [0 1] [0 2] [1 2] [2 2] [2 1] [2 0]
                 ; followed by some miscellaneous interior points...
                 [1 1]]
          expected [[2 2] [0 2] [0 0] [2 0]]]
      (is (polygon== expected (convex-hull nodes)))))
  (testing "diamond"
    (let [nodes [[0 10] [4 5] [8 0] [4 -5] [0 -10] [-4 -5] [-8 0] [-4 5]
                 ; followed by some miscellaneous interior points...
                 [1 1] [-1 -1] [-1 1] [1 -1]]
          expected [[0 10] [-8 0] [0 -10] [8 0]]]
      (is (polygon== expected (convex-hull nodes)))))
  (testing "octogon"
    (let [nodes [[-5 -2] [-5 -1] [-5 0] [-5 1] [-5 2]
                 [-4 3] [-3 4]
                 [-2 5] [-1 5] [0 5] [1 5] [2 5]
                 [3 4] [4 3]
                 [5 2] [5 1] [5 0] [5 -1] [5 -2]
                 [4 -3] [3 -4]
                 [2 -5] [1 -5] [0 -5] [-1 -5] [-2 -5]
                 [-3 -4] [-4 -3]
                 ; followed by some miscellaneous interior points...
                 [-3 3] [-3 -3] [3 3] [3 -3] [0 0]]
          expected [[2 5] [-2 5] [-5 2] [-5 -2] [-2 -5] [2 -5] [5 -2] [5 2]]]
      (is (polygon== expected (convex-hull nodes)))))
  (testing "https://en.wikibooks.org/wiki/Algorithm_Implementation/Geometry/Convex_hull/Monotone_chain#Clojure"
    (let [nodes '([0 0] [0 1] [0 2] [0 3]
                  [1 0] [1 1] [1 2] [1 3]
                  [2 0] [2 1] [2 2] [2 3] [2 4] [2 5] [2 6] [2 7] [2 8] [2 9]
                  [3 0] [3 1] [3 2] [3 3]
                  [4 0] [4 1] [4 2] [4 3] [4 4]
                  [5 1])
          expected '([0 0] [0 3] [2 9] [4 4] [5 1] [4 0])]
      (is (polygon== expected (convex-hull nodes))))))

(deftest star-test
  (testing "Star test from: https://people.sc.fsu.edu/~jburkardt/m_src/convex_hull_test/convex_hull_test.html"
    (let [nodes [[ 0.95105651629515353       0.30901699437494740 ]
                 [ 0.22451398828979272       0.30901699437494740 ]
                 [-0.95105651629515353       0.30901699437494751 ]
                 [-0.36327126400268051      -0.11803398874989464 ]
                 [ 0.58778525229247292      -0.80901699437494756 ]
                 [ 0.36327126400268039      -0.11803398874989492 ]
                 [ 0.0                       1.0000000000000000 ]
                 [-0.22451398828979263       0.30901699437494745 ]
                 [-0.58778525229247325      -0.80901699437494734 ]
                 [ 0.0                      -0.38196601125010515]]
          expected [[ 0.58778525229247292      -0.80901699437494756 ]
                    [-0.58778525229247325      -0.80901699437494734 ]
                    [-0.95105651629515353       0.30901699437494751 ]
                    [ 0.0                       1.0000000000000000 ]
                    [ 0.95105651629515353       0.30901699437494740 ]]]
      (is (polygon== expected (convex-hull nodes))))))
